package com.zhihuishu.micro.course.service.impl;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import redis.clients.jedis.Jedis;

import com.zhihuishu.micro.course.dao.CourseClipsMapper;
import com.zhihuishu.micro.course.dto.CourseClipsDto;
import com.zhihuishu.micro.course.helper.LogModule;
import com.zhihuishu.micro.course.helper.RemoteLogHelper;
import com.zhihuishu.micro.course.model.CourseClips;
import com.zhihuishu.micro.course.service.CourseClipsService;
import com.zhihuishu.micro.course.service.CourseService;
import com.zhihuishu.toolkit.helper.BeanHelper;
import com.zhihuishu.toolkit.jedis.JedisHelper;
import com.zhihuishu.toolkit.jedis.JedisHelper.Handler;
import com.zhihuishu.toolkit.jedis.template.JedisExecutor;

@Service("courseClipsService")
public class CourseClipsServiceImpl extends BaseService implements CourseClipsService {

	@Resource
	private CourseClipsMapper courseClipsMapper ;
	@Resource
	private CourseService courseService ;

	 @Resource
	 private JedisExecutor jedisExecutor;
	
	@Override
	public CourseClipsDto get(final long clipsId) {
		final CourseClipsDto target = new CourseClipsDto() ;
		jedisExecutor.execute( new Handler() {
			@Override
			public void handle(Jedis jedis) {
				final String key = JedisHelper.key("cc:course:clips" ,clipsId) ;
				CourseClipsDto dto = null ;
				Map<String ,String> map = jedis.hgetAll(key) ;
				if(!CollectionUtils.isEmpty(map) && map.get("clipsId") != null) {
					dto = BeanHelper.transTo(map, CourseClipsDto.class) ;
				} else {
					dto = toClipsDto(courseClipsMapper.get(clipsId)) ;
					if(dto != null && dto.getClipsId() != null) {
						map = BeanHelper.describe(dto) ;
						jedis.hmset(key, map) ;
					}
				}
				if(dto != null && dto.getClipsId() != null) {
					BeanHelper.copyProperties(dto, target);
				}
			}
		});
		return target.getClipsId() != null ? target : null ;
	}

	/**
	 * 类型转换，课程ID使用TBL_COURSE主键
	 * @param clips
	 * @param courseId
	 * @return
	 */
	private CourseClipsDto toClipsDto(CourseClips clips) {
		if(clips == null) return null ;
		CourseClipsDto dto = new CourseClipsDto() ;
		dto.setClipsId(clips.getId());
		dto.setDescription(clips.getMovieDesc());
		dto.setUserAvatar(clips.getMoviePhoto());
		dto.setUserId(clips.getMovieUserid());
		dto.setUserInfo(clips.getMovieAuthorInfo());
		dto.setUserLevel(clips.getMovieLevel());
		dto.setUserName(clips.getMovieUsername());
		dto.setVideoId(clips.getVideoId());
		dto.setVideoImage(clips.getMovieClipsImg());
		dto.setVideoPath(clips.getMovieClips());
		return dto;
	}

	@Override
	public void update(final CourseClipsDto clips ,final long updateUser) {
		Assert.notNull(clips.getClipsId() ,"片花ID(CC_COURSE_ID)为空!");
		RemoteLogHelper.log(LogModule.COURSE_CLIPS_UPDATE.getCode() ,updateUser ,"[开始]更新课程片花({})" ,clips.getClipsId());
		CourseClips model = toClipsModel(clips) ;
		if(model != null) {
			courseClipsMapper.update(model);
			// 更新后，删除缓存
			jedisExecutor.execute( new Handler() {
				@Override
				public void handle(Jedis jedis) {
					final String key = JedisHelper.key("cc:course:clips" ,clips.getClipsId()) ;
					jedis.del(key) ;
				}
			});
		}
		RemoteLogHelper.log(LogModule.COURSE_CLIPS_UPDATE.getCode() ,updateUser ,"[完成]更新课程片花({})" ,clips.getClipsId());
	}

	private CourseClips toClipsModel(CourseClipsDto clips) {
		if(clips == null) return null ;
		CourseClips model = new CourseClips() ;
		model.setId(clips.getClipsId());
		model.setMovieDesc(clips.getDescription());
		model.setMoviePhoto(clips.getUserAvatar());
		model.setMovieUserid(clips.getUserId());
		model.setMovieAuthorInfo(clips.getUserInfo());
		model.setMovieLevel(clips.getUserLevel());
		model.setMovieUsername(clips.getUserName());
		model.setVideoId(clips.getVideoId());
		model.setMovieClipsImg(clips.getVideoImage());
		model.setMovieClips(clips.getVideoPath());
		model.setUpdateTime(new Date());
		return model;
	}

	@Override
	public Long save(long courseId ,CourseClipsDto clips ,long createUser) {
		// 根据课程ID查询CC_COURSE_ID，此ID暂为片花ID
		Long linkId = courseService.getLinkCourseId(courseId) ;
		if(linkId == null || linkId.equals(0L)) return null ;
		clips.setClipsId(linkId);
		update(clips ,createUser) ;	// 由于目前数据结构的原因(数据库表设计)，创建使用更新方法来做，主键为课程ID
		return linkId ;
	}

}
